package com.techsoul.module.marketing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.security.pojo.LoginUser;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.marketing.mapper.CouponActivityMapper;
import com.techsoul.module.marketing.pojo.entity.CouponActivity;
import com.techsoul.module.marketing.pojo.param.CouponActivitySaveParam;
import com.techsoul.module.marketing.pojo.param.CouponActivitySearchParam;
import com.techsoul.module.marketing.pojo.param.CouponSearchParam;
import com.techsoul.module.marketing.pojo.vo.CouponActivityVo;
import com.techsoul.module.marketing.pojo.vo.CouponVo;
import com.techsoul.module.marketing.service.ICouponActivityService;
import com.techsoul.module.marketing.service.ICouponGoodsRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 优惠券活动 Service实现
 * 
 * @author liuen
 * @date 2021-04-22
 */
@Service
@Transactional(readOnly = true)
public class CouponActivityServiceImpl implements ICouponActivityService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private CouponActivityMapper mapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ICouponGoodsRelService couponGoodsRelService;

    /**
     * 新增
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public String insert(CouponActivitySaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        CouponActivity entity = new CouponActivity();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        this.mapper.insert(entity);
        // 新增优惠券商品关系
        if (ListUtils.isNotEmpty(params.getCouponGoodsRelList())) {
            params.getCouponGoodsRelList().forEach(couponGoodsRel -> {
                couponGoodsRel.setActivityId(entity.getId())
                        .setActivityName(entity.getActivityName())
                        .setStoreId(entity.getStoreId());
                couponGoodsRelService.insert(couponGoodsRel);
            });
        }
        return entity.getId();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param idList
     */
    @Transactional(readOnly = false)
    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    /**
     * 修改
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public Integer update(CouponActivitySaveParam params) {
        CouponActivity entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(user.getId());
        return this.mapper.updateById(entityFromDb);
    }

    /**
     * 修改优惠券活动商品关系
     * @param params
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public void updateCouponGoodsRels(CouponActivitySaveParam params) {
        // 更新优惠券活动
        this.update(params);
        // 更新优惠券活动商品关系，全删全入
        couponGoodsRelService.deleteByActivityId(params.getId());
        if (ListUtils.isNotEmpty(params.getCouponGoodsRelList())) {
            params.getCouponGoodsRelList().forEach(couponGoodsRel -> {
                couponGoodsRel.setActivityId(params.getId())
                        .setActivityName(params.getActivityName())
                        .setStoreId(params.getStoreId());
                couponGoodsRelService.insert(couponGoodsRel);
            });
        }
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public CouponActivityVo findById(String id) {
        return this.mapper.findById(id);
    }

    /**
     * 列表
     * @param params
     * @return
     */
    @Override
    public List<CouponActivityVo> findAll(CouponActivitySearchParam params) {
        return this.mapper.findAll(params);
    }

    /**
     * 列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<CouponActivityVo> findAll(CouponActivitySearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }

    /**
     * 店铺内可领取的优惠券列表
     * @return
     */
    @Override
    public List<CouponActivityVo> unRecivedCouponList(CouponActivitySearchParam params) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
            params.setReceiverId(user.getId());
        }
        return this.mapper.unRecivedCouponList(params);
    }

}
